Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
TypeScript Type Hoisting "chicken or egg" problem

Before someone marks this as a duplicate, I checked 2 other questions that didn't actually solve anything. They were only ideas for alternatives ways to do the exact problem, but didn't show a solution to my problem.

When making an array in TypeScript, I assigned the type any[] to it, since I haven't imported the type of the array yet. However, I have to pass this array to a require statement to make class methods work on this particular array.

If I make the array first, (using any[]) then I won't be able to type the array.

let blockchain: any[] = [];

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain);
type BlockType = InstanceType<typeof Block>;

// How do I set blockchain's type to BlockType[]?

However, if I import the classes first, I won't be able to pass the array to give the correct array to class methods

// Pass array to import classes and functions
const {/* stuff to import */} = require("./otherfile.js")(blockchain); // error
type BlockType = InstanceType<typeof Block>;

let blockchain: BlockType[] = [];

Importing the module twice, first with an empty array just to obtain the class, is not an option for me, since the original import's class methods will push data to the empty array and it'll be pointless.

EDIT: To be more specific about the module classes, this is what happens inside the other file.

function exp(blockchain: any[]): Object {
    class Block {
        // public x: number, etc...
        
        constructor(
            // stuff...
        }

        static generate(/* params */): Block {
            // do stuff, edit properties...

            let block = new Block(/* args */);
            blockchain.push(block); // Array that was passed to module
            return block;
        }
    }
    
    return {
        Block: Block
    }
}

Scotty Jamison's suggestion wouldn't work here because the Block class from import #1 wouldn't be the same as the one from import #2, and their methods would push to different arrays.

let {Block1} = require("./otherfile.js")([]);
let blockchain: Block1[] = [];
let {Block2} = require("./otherfile.js")(blockchain);

Block1.generate("yo");
Block2.generate("foobar");

console.log(blockchain.length); // 1
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda